import platform
import config.ConfigDefault as default
from config import joeDevice as JD


class Dict(dict):
    def __init__(self, d):
        super(Dict, self).__init__(d)
        for k, v in d.items():
            self[k] = v

    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError:
            raise AttributeError(r"'Dictionary' object has no attribute '%s'" % key)

    def __setattr__(self, key, value):
        self[key] = value


def getUrl(configs):
    envir = configs['test_environment']
    if envir.lower() == 'dev-china':
        url = configs.get('dev-china_test_url')
    elif envir.lower() == 'qa':
        url = configs.get('qa_test_url')
    elif envir.lower() == 'stage':
        url = configs.get('stage_test_url')
    else:
        url = configs.get('pro_test_url')

    configs['test_url'] = url
    return configs


def merge(default, override):
    configs = default.copy()
    for key in override:
        if isinstance(override[key], dict):
            config = merge(default[key], override[key])
        elif override[key].replace(' ', '') != '':
            config = override[key]
        else:
            config = default[key]
        configs[key] = config

    return configs


def toDict(d):
    dict1 = Dict()
    for k, v in d.items():
        if isinstance(v, dict):
            dict1[k] = toDict(v)
        else:
            dict1[k] = v
    return dict1


configs = default.configs
configs_mac = default.mac_configs
if platform.system() == 'Darwin':
    configs = merge(configs, configs_mac)

configs = getUrl(configs)  # 配置文件数据

devices = {**JD.device_joe}

if __name__ == '__main__':
    pass
